﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Q_Link
{
    public partial class frmSettings : Form
    {
        frmMain mainForm;
        string configFile = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + "Config.xml";

        public frmSettings(frmMain main)
        {
            InitializeComponent();
            mainForm = main;
            initializeDatagridFolders(main.folderList);
        }

        void initializeDatagridFolders(List<string> folderList)
        {
            dataGridViewFolders.Rows.Clear();
            foreach (string folder in folderList)
            {
                dataGridViewFolders.Rows.Add(folder);
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frmAddFolder addFolder = new frmAddFolder(this);
            addFolder.Show();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridViewFolders.SelectedRows.Count > 0)
            {
                int selectedIndex = dataGridViewFolders.SelectedRows[0].Index;
                DataGridViewRow rowToDelete = dataGridViewFolders.Rows[selectedIndex];

                // gets the RowID from the first column in the grid
                string oldFolder = dataGridViewFolders[0, selectedIndex].Value.ToString();
                // delete folder from configfile
                deleteFromConfigFile(oldFolder);
                // delete form folderlist
                mainForm.folderList.Remove(oldFolder);
                // delete row from datagridview
                dataGridViewFolders.Rows.Remove(rowToDelete);
            }
        }

        void deleteFromConfigFile(string oldFolder)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(configFile);

            //Select the book node with the matching attribute value.
            XmlNode folder;
            XmlElement root = xDoc.DocumentElement;
            folder = root.SelectSingleNode("descendant::Folder[@path='" + oldFolder + "']");
            if (folder != null)
            {
                folder.ParentNode.RemoveChild(folder);
                xDoc.Save(configFile);
            }
            else
            {
                MessageBox.Show(oldFolder + " node niet gevonden.");
            }
        }

        // add folder to config.xml
        public void addFolder(string newFolder)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(configFile);
                XmlNode xNode = xDoc.CreateNode(XmlNodeType.Element, "Folder", "");
                XmlAttribute xKey = xDoc.CreateAttribute("path");
                xKey.Value = newFolder;
                xNode.Attributes.Append(xKey);
                xDoc.GetElementsByTagName("ShortcutFolders")[0].InsertAfter(xNode,
                    xDoc.GetElementsByTagName("ShortcutFolders")[0].LastChild);
                xDoc.Save(configFile);

                // add folder to folderlist
                mainForm.folderList.Add(newFolder);
                // add folder to datagridview
                dataGridViewFolders.Rows.Add(newFolder);
            }
            catch
            {
                MessageBox.Show("Opslaan mislukt.");
            }
        }

        private void openFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridViewFolders.SelectedRows.Count == 1)
            {
                string selectedFolder = dataGridViewFolders.SelectedRows[0].Cells[0].Value.ToString();
                // MessageBox.Show(selectedFolder);
                try
                {
                    Process.Start(selectedFolder);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            mainForm.generateShortcutList();
            this.Close();
        }
    }
}
